package com.zhongzhi.javafx.controller;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;

import com.zhongzhi.common.Common;
import com.zhongzhi.common.GrabDetail;
import com.zhongzhi.thread.TaskTherad;
import com.zhongzhi.util.ClientUtil;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import lombok.extern.log4j.Log4j;
@Log4j
public class detailController implements Initializable{
	@FXML
	private ImageView image;
	@FXML
	private Label station;		//工位
	@FXML
	private Label Type;			//机型
	@FXML
	private Label Num;			//机号
	@FXML
	private Label StartTime;	//开始时间
	@FXML
	private Label Run;			//当前状态
	@FXML
	private Label ResTime;		//剩余时间
	@FXML
	private Label StartHA;		//开始高度A
	@FXML
	private Label CurHA;		//当前高度A
	@FXML
	private Label DisHA;		//当前下垂量A
	@FXML
	private Label StateA;		//预计下垂量A
	@FXML
	private Label StartHB;		//开始高度B
	@FXML
	private Label CurHB;		//当前高度B
	@FXML
	private Label DisHB;		//当前下垂量B
	@FXML
	private Label StateB;		//预计下垂量B
	@FXML
	private Button Stop;		//停止任务
	@FXML
	private Button Return;		//返回窗口
	@FXML
	private Label aoh;			//a测试显示文字
    @FXML
    private Label boh;			//b测试显示文字

	private Timer timer = new Timer();
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		log.info("详细工位测试信息:开始初始化");
		Stop.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Integer selectStation = Common.getSelectStation();
				GrabDetail grabDetail = Common.getPlc().getMap().get(selectStation);
				String string = "http://127.0.0.1:8080/doosanRadar/ds/enddetection?station=" + grabDetail.getStationName();
				log.info("详细工位测试信息:发送停止任务请求:"+string);
				String gethttp = ClientUtil.Gethttp(string);
				log.info("详细工位测试信息:接收到返回参数:"+gethttp);
			}
		});



		//返回窗口
		Return.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				log.info("详细工位测试信息:按下返回按钮");
				ScrollPane sp = Common.getSp();
				sp.setContent(Common.getTp());
				timer.cancel();
			}
		});


		//按时刷新
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				Platform.runLater(new Runnable() {
					@Override
					public void run() {
						Res();
					}
				});
			}
		}, 100,500);
		log.info("详细工位测试信息:初始化完成");
	}
	//刷新
	private void Res() {
		Integer selectStation = Common.getSelectStation();
		GrabDetail grabDetail = Common.getPlc().getMap().get(selectStation);
		if (grabDetail!=null) {
			Integer stationId = grabDetail.getId();
			TaskTherad taskTherad = Common.getTaskTherads().get(stationId);
			String type = taskTherad.getType();		//机型
			String num = taskTherad.getNum();		//机号

			Long startTime = taskTherad.getStartTime();		//开始时间
			Long timeLong = taskTherad.getTimeLong();		//运行时间
			Boolean run = taskTherad.getRun();				//运行状态
			Boolean stop2 = taskTherad.getStop();			//强行停止
			Long resTime = taskTherad.getResTime();			//剩余时间

			Double startHA = taskTherad.getStartHA();		//开始
			Double CurHA = taskTherad.getCurHA();			//当前
			Double DisHA = taskTherad.getDisHA();			//预计
			String StateA = taskTherad.getStateA();			//范围

			Double startHB = taskTherad.getStartHB();
			Double CurHB = taskTherad.getCurHB();
			Double DisHB = taskTherad.getDisHB();
			String StateB = taskTherad.getStateB();

			if (run==false && stop2==false) {
				aoh.setText("结束高度");
				boh.setText("结束高度");
			}
			if(run==true) {
				aoh.setText("当前高度");
				boh.setText("当前高度");
			}
			
			SetText(this.station,grabDetail.getStationName());
			SetText(this.Type,type);
			SetText(this.Num,num);
			SetText(this.StartTime,grabDetail.getStartTime());
			SetTextColour(this.Run, run, "运行中","运行完毕", "未开始");
			SetText(this.ResTime,grabDetail.getResTime());
			if (startHA!=null) {
				SetText(this.StartHA,String.format("%.1f", startHA));
			}
			if (CurHA!=null) {
				SetText(this.CurHA,String.format("%.1f", CurHA));
			}
			if (DisHA!=null) {
				SetText(this.DisHA,String.format("%.1f", DisHA));
			}
			this.StateA.setText(StateA);
			if (startHB!=null) {
				SetText(this.StartHB,String.format("%.1f", startHB));
			}
			if (CurHB!=null) {
				SetText(this.CurHB,String.format("%.1f", CurHB));
			}
			if (DisHB!=null) {
				SetText(this.DisHB,String.format("%.1f", DisHB));
			}
			this.StateB.setText(StateB);

		}
	}


	private void SetText(Label label,String s) {
		if (s==null) {
			label.setText("暂无");
		}else {
			label.setText(s);
		}
	}
	private void SetTextColour(Label label,Boolean bool,String run,String stop,String open) {

		if (bool==null) {
			label.setText(open);
			label.setTextFill(Paint.valueOf("#000000"));
		}else if (bool==true) {
			label.setText(run);
			label.setTextFill(Paint.valueOf("#00cf00"));
		}else{
			label.setText(stop);
			label.setTextFill(Paint.valueOf("#cf0000"));
		}
	}
}
